package org.zhucy.jdk8;


import com.sun.org.apache.xpath.internal.SourceTree;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by zcy on 2018/2/6.
 *
 * @version 1.0
 */
public class AppRun {

    public static void main(String[] args) {
        isNullAdd();
    }

    /**
     * partitioningBy 收集器 根据true|false 收集
     */
    public void partitioningBy(){
        List<Cat> catList = getCatList();
        Map<Boolean,List<Cat>> listMap = catList.stream().collect(Collectors.partitioningBy(cat -> cat.getAge() >2));
        System.out.println(listMap.get(true).size());
        System.out.println(listMap.get(false).size());
    }

    /**
     * maxBy 计算最大值
     */
    public static void maxBy(){
        List<Cat> catList = getCatList();
        Optional<Cat> optional = catList.stream().collect(Collectors.maxBy(Comparator.comparing(Cat::getAge)));
        System.out.println(optional.get().getAge());

    }

    /**
     * averagingDouble 计算平均数
     */
    public static void averagingDouble(){
        List<Cat> catList = getCatList();
        Double aveAge = catList.stream().collect(Collectors.averagingDouble(Cat::getAge));
        System.out.println(aveAge);
    }

    /**
     * groupingBy 分组
     */
    public static void groupingBy(){
        List<Cat> catList = getCatList();
        Map<String,List<Cat>> listMap = catList.stream().collect(Collectors.groupingBy(Cat::getColor));
        System.out.println(listMap.get("red").size());
    }

    /**
     * 组合收集器（分组并计数）
     */
    public static void groupingByAndCount() {
        List<Cat> catList = getCatList();
        Map<Integer,Long> longMap = catList.stream().collect(Collectors.groupingBy(Cat::getAge,Collectors.counting()));
        System.out.println(longMap.get(3));
    }

    /**
     * 组合收集器（控制组内容）
     */
    public static void groupingByAndColor() {
        List<Cat> catList = getCatList();
        Map<Integer,List<String>> longMap = catList.stream().collect(Collectors.groupingBy(Cat::getAge,Collectors.mapping(Cat::getColor,Collectors.toList())));
        longMap.get(3).forEach(s -> System.out.println(s));
    }

    /**
     * 拼接字符串
     * @since 1.8
     */
    public static void joining(){
        List<Cat> catList = getCatList();
        String result = catList.stream().map(Cat::getColor).collect(Collectors.joining(",","[","]"));
        System.out.println(result);
    }

    public static void isNullAdd(){
        Map<String,Cat> catMap = new HashMap<>();
        catMap.put("z",new Cat("b",1,"green","music"));
        catMap.put("c",null);
        Cat cat = catMap.computeIfAbsent("c",k->getCat(k));
        System.out.println(cat.getAge());
        Cat c = catMap.computeIfPresent("b",(k,v)->getCat(k));
        System.out.println(c.getAge());
    }

    public static Cat getCat(String key){
       return new Cat(key,0,"red","study");
    }


    public static List<Cat> getCatList(){
        List<Cat> catList = new ArrayList<>();
        catList.add(new Cat("a",0,"red","study"));
        catList.add(new Cat("b",1,"green","music"));
        catList.add(new Cat("c",2,"red","foot"));
        catList.add(new Cat("d",3,"yellow","movie"));
        catList.add(new Cat("d",3,"yellow","movie"));
        return catList;
    }
}
